<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :model="state.param" :inline="true">
					<el-form-item label="信息搜索">
						<el-input v-model="state.param.seckillManagerName" placeholder="请输入关键字搜索" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="search('search')">
							<el-icon>
								<ele-Search />
							</el-icon>
							搜 索
						</el-button>
						<el-button type="primary" @click="search('reset')">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重 置
						</el-button>
						<el-button type="primary" @click="add('add')">
							<el-icon>
								<ele-Plus />
							</el-icon>
							新 增
						</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex; flex-direction: column; flex: 1; height: inherit">
				<section>
					<el-table :data="state.tableData">
						<el-table-column type="index" label="ID" width="90" />
						<el-table-column prop="id" label="轮播图" show-overflow-tooltip width="90">
							<template #default="scope">
								<el-image style="width: 50px" :src="showslidsImages(scope.row.seckillManagerSilderImgs)" />
							</template>
						</el-table-column>
						<el-table-column label="秒杀名称" show-overflow-tooltip class-name="textLeft">
							<template #default="scope">
								<span class="text">
									{{ scope.row.seckillManagerName }}
								</span>
							</template>
						</el-table-column>
						<el-table-column prop="seckillManagerTime" label="秒杀时段" width="140">
							<template #default="scope">
								{{ tableTimerange(scope.row.seckillManagerTime) }}
							</template>
						</el-table-column>
						<el-table-column prop="seckillManagerStatus" label="状态">
							<template #default="scope">
								<el-switch v-model="scope.row.seckillManagerStatus" @change="statusChange(scope.row)" :active-value="1" :inactive-value="0" />
							</template>
						</el-table-column>
						<el-table-column prop="createTime" label="创建时间">
							<template #default="scope">
								{{ createTimeCom(scope.row.createTime) }}
							</template>
						</el-table-column>
						<el-table-column prop="createBy" label="创建人"></el-table-column>
						<el-table-column label="操作" fixed="right" width="120">
							<!-- <template #default="scope">
								<el-button link type="primary" @click="onOpenDetail(scope.row)">添加商品</el-button>
								&nbsp;|&nbsp;
								<el-button link type="primary" @click="onOpenEdit(scope.row)">编辑</el-button>
								<el-button link type="primary" @click="onRowDel(scope.row)">删除</el-button>
							</template> -->
							<template #default="scope">
								<span class="pointer font16" style="color: var(--el-color-primary)" @click="onOpenEdit(scope.row)"> 编辑 </span>
								<span style="color: var(--el-color-primary)">&nbsp;|&nbsp;</span>
								<el-dropdown>
									<span style="color: var(--el-color-primary)" class="pointer font16"> 操作 </span>
									<template #dropdown>
										<el-dropdown-menu>
											<el-dropdown-item @click="onOpenDetail(scope.row)">添加商品</el-dropdown-item>
											<el-dropdown-item @click="onRowDel(scope.row)">删除</el-dropdown-item>
										</el-dropdown-menu>
									</template>
								</el-dropdown>
							</template>
						</el-table-column>
					</el-table>
				</section>
				<el-row justify="end">
					<el-col :span="24" class="flex-end-center">
						<el-pagination
							@size-change="onHandleSizeChange"
							@current-change="onHandleCurrentChange"
							class="mt15"
							:pager-count="5"
							:page-sizes="[10, 20, 30]"
							v-model:current-page="state.param.current"
							background
							v-model:page-size="state.param.size"
							layout="total, sizes, prev, pager, next, jumper"
							:total="state.total"
						>
						</el-pagination>
					</el-col>
				</el-row>
			</el-card>
		</div>
		<addCategoryDialog ref="addCategoryDialogRef" :editeItem="editeItem" @refresh="refreshList" />
	</div>
</template>

<script setup lang="ts" name="saleIndex">
import { reactive,onMounted,ref, defineAsyncComponent, computed} from 'vue';
import { useRouter } from 'vue-router';
import type { saleOrderListData } from "/@/types/marketing";
import { seckillList, schemeDel, seckillDelete, changeSeckillActionStatus } from '/@/api/marketing';
import { ElMessageBox, ElMessage } from 'element-plus';
const addCategoryDialog = defineAsyncComponent(() => import('/@/views/marketing/seckillManage/config/components/addConfigDialog.vue'));
// 定义变量内容
const addCategoryDialogRef = ref();

// 编辑传值
const editeItem = reactive({});

const router = useRouter();
// 定义变量内容
const state = reactive({
	param: {
		dateTime:[],
		startTime: "",// 开始时间
		endTime: "",// 结束时间
		keyword: "",
		pageNum: 1,
		pageSize: 10,  //这样就可以了， 但是这个不是自动切换得嘛 上面绑定了值
		status: "null",
		seckillManagerName: "",
		current: 1,
		size: 10,  //当前页面展示的条数默认是10条
	},
	query: {
		orderStatus: [
			{ value: 0, label: '进行中' },
			{ value: 2, label: '已过期' },
			{ value: 3, label: '已删除' }
		],
	},
	loading: false,
	total: 0,
	tableData: [] as Array<saleOrderListData>
});
const tableTimerange = computed(()=>{
	return function(val:string){
		return val?.replace(',','-');
	}
})
const createTimeCom = computed(()=>{
	return function(val: number){
		const time = new Date(val);
		return time.getFullYear() + '/' + ( time.getMonth() + 1 ) + '/' + time.getDate();
	}
})

const showslidsImages = computed(()=>{
	return function(val:string[]){
		if(val.length >= 1){
			return val[0]
		} else {
			return ''
		}
	}
})


// 获取订单列表
const getSeckillList = () => {
	state.loading = true;//不用传参， 请求得入参在上面都定义了 param这里吗？
	seckillList(state.param).then(res => {
		const { results:{current, size, records, total} } = res;
		state.loading = false;
		state.tableData = records;
		state.total = total;
	});
}
onMounted(() => {
	getSeckillList();
})

//刷新列表
const refreshList = () => {
	getSeckillList();
}
// 搜索
const search = (type:string) => {
	if(type == 'reset') {
		state.param.seckillManagerName = '';
	}
	state.param.current = 1;
	getSeckillList();
}

// 打开新增编辑分类弹窗
const add = (type: string, row ?: Object) => {
	addCategoryDialogRef.value.openDialog(type, JSON.parse(JSON.stringify(row)));}
// 新增秒杀活动弹窗
// const add = (type: string) => {
// 	addCategoryDialogRef.value.openDialog(type);
// };
// 编辑秒杀活动弹窗
const onOpenEdit = (item: any) => {
	Object.assign(editeItem,item);
	addCategoryDialogRef.value.openDialog('edit');
};


// 订单详情
const onOpenDetail = (item: any) => {
	// console.log(item.id)
	router.push({
		path: '/marketing/seckillManage/goods/add',
		// query: { id: item.id },
	});
};

// 分页点击 条数
const onHandleSizeChange = (val: number) => {
	state.param.size = val;
	getSeckillList();
};
// 分页点击
const onHandleCurrentChange = (val: number) => {
	state.param.current = val;
	getSeckillList();
};

// 删除活动
const onRowDel = (row:any) => {
	ElMessageBox.confirm(`此操作将删除该配置，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then( () => {
		seckillDelete(row.seckillManagerId).then(res => {
			const { code, message } = res;
			if(code == 400){
				ElMessage.success('删除成功');
				getSeckillList();
			}
		})
	});
};
// 修改状态
const statusChange = (obj : any) => {
	console.log(obj)
	changeSeckillActionStatus({
		seckillManagerId:Number( obj.seckillManagerId ),
		seckillManagerStatus:obj.seckillManagerStatus == 0 ? 1 : 0
	}).then(res =>{
		if(res.code == 400){
			ElMessage.success('修改成功');
		}
	})
}
</script>
<style scoped lang="scss">
:deep(.el-card__body) {
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;
	.el-table {
		flex: 1;
		.el-table__inner-wrapper::before {
			background: none !important;
		}
	}
}
.textLeft{
	.text{
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		word-break: break-all;
	}
}
:deep(.el-table .textLeft .cell.el-tooltip ){
	justify-content: flex-start !important;
}
:deep(.el-table th.textLeft .cell ){
	justify-content: flex-start !important;
	padding: 0 !important;
}
</style>
<style>
.el-table.is-scrolling-none th.el-table-fixed-column--right {
	background-color: var(--el-color-primary-light-8) !important;
}
</style>
